// Package layer_range_tree 4-637
// @Title layer_range_tree
// @Description
// @Author  == 琳琳也叫王木木 ==  2021/9/16 14:13
// @Create  2021/9/16 14:13
package layer_range_tree

func averageOfLevels(root *TreeNode) []float64 {
	if root == nil {
		return nil
	}
	ans := []float64{}
	queue := []*TreeNode{root}
	for len(queue) > 0 {
		size := len(queue)
		sum := 0
		count := 0
		for i := 0; i < size; i++ {
			count++
			node := queue[0]
			queue = queue[1:]
			sum += node.Val
			if node.Left != nil {
				queue = append(queue, node.Left)
			}
			if node.Right != nil {
				queue = append(queue, node.Right)
			}
		}
		ans = append(ans, float64(float64(sum)/float64(count)))
	}
	return ans
}
